package org.millioncall.yueyoga.admin.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.millioncall.openplatform.common.PaginatedList;
import org.millioncall.openplatform.util.DateTimeUtils;
import org.millioncall.yueyoga.admin.dao.GroupsDAO;
import org.millioncall.yueyoga.admin.model.Groups;
import org.millioncall.yueyoga.admin.service.GroupsService;

/**
 * @author lisj Apr 12, 2014 2:14:19 PM
 */
public class GroupsServiceImpl implements GroupsService {

	private GroupsDAO groupsDAO;
	private Map<String, Object> map = new HashMap<String, Object>();;

	@Override
	public boolean saveGroups(Groups groups) {
		map = new HashMap<String, Object>();
		if (groups != null) {
			if (StringUtils.isNotBlank(groups.getName())) {
				map.put("name", groups.getName());
				map.put("createTime",
						DateTimeUtils.getInstance().format(new Date(),
								"yyyy-MM-dd HH:mm:ss"));
			}
			if (StringUtils.isNotBlank(groups.getRemarks())) {
				map.put("remarks", groups.getRemarks());
			}
			if (StringUtils.isNotBlank(groups.getHallCode())) {
				map.put("hallCode", groups.getHallCode());
			}
			if (!map.isEmpty()) {
				return groupsDAO.saveGroups(map) > 0 ? true : false;
			}
		}
		return false;
	}

	@Override
	public boolean deleteGroupsById(Integer id) {
		return groupsDAO.deleteGroupsById(id) > 0 ? true : false;
	}

	@Override
	public boolean updateGroupsById(Groups groups) {
		map.clear();
		if (groups != null) {
			if (StringUtils.isNotBlank(groups.getName())) {
				map.put("name", groups.getName());
			}
			if (StringUtils.isNotBlank(groups.getRemarks())) {
				map.put("remarks", groups.getRemarks());
			}
			map.put("id", groups.getId());
			if (!map.isEmpty() && groups.getId() != null) {
				return groupsDAO.updateGroupsById(map) > 0 ? true : false;
			}
		}
		return false;
	}

	@Override
	public Groups findGroupsById(Integer id) {
		return groupsDAO.findGroupsById(id);
	}

	@Override
	public List<Groups> findGroupsByOrganizationId(String organizationId) {
		return groupsDAO.findGroupsByOrganizationId(organizationId);
	}

	@Override
	public PaginatedList<Groups> findMemberGroupsByCdts(
			Map<String, Object> map, int start, int limit) {
		if (!map.isEmpty()) {
			return groupsDAO.findMemberGroupsByCdts(map, start, limit);
		}
		return null;
	}

	@Override
	public boolean deleteGroupsByIds(String[] groupIds) {
		int n = 0;
		if (groupIds != null && groupIds.length != 0) {
			for (String groupId : groupIds) {
				if (StringUtils.isNotBlank(groupId)) {
					n += groupsDAO.deleteGroupsById(Integer.valueOf(groupId));
				} else {
					return false;
				}
			}
			return n > 0 ? true : false;
		}
		return false;
	}

	public GroupsDAO getGroupsDAO() {
		return groupsDAO;
	}

	public void setGroupsDAO(GroupsDAO groupsDAO) {
		this.groupsDAO = groupsDAO;
	}
}
